﻿using _RobotInspection.DTO;
using _RobotInspection.Entities;
using _RobotInspection.Interfaces;
using _RobotInspection.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using SqlSugar;
using System.IO;
using System.Net.Mime;

namespace _RobotInspection.Controllers
{
    /// <summary>
    /// 设备相关控制层
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class EquipmentController : ControllerBase
    {
        private readonly IEquipmentService _service;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        public EquipmentController(IEquipmentService service)
        {
            _service = service;
        }

        /// <summary>
        /// 分页获取设备列表
        /// </summary>
        /// <param name="queryDto"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<ApiResponse<EquipmentPagedResultDto>>> GetEquipmentsPaged([FromQuery] EquipmentPagedQueryDto queryDto)
        {
            var result =  await _service.GetEquipmentsPagedAsync(queryDto);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 根据id查询设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResponse<equipment>>> GetEquipmentById([FromRoute] long id)
        {
            var result = await _service.GetEquipmentByIdAsync(id);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 根据设备id查询设备资料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResponse<List<equipment_resource>>>> GetEquipmentResourceByEquipmentId([FromRoute] long id)
        {
            var result = await _service.GetEquipmentResourceByEquipmentIdAsync(id);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 根据id查询设备资料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResponse<equipment_resource>>> GetEquipmentResourceById([FromRoute] long id)
        {
            var result = await _service.GetEquipmentResourceByIdAsync(id);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 创建设备
        /// </summary>
        /// <param name="queryDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<ApiResponse<object>>> InsertEquipment([FromQuery] EquipmentInsertDto queryDto)
        {
            var result = await _service.InsertEquipmentAsync(queryDto);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 创建设备资料
        /// </summary>
        /// <param name="queryDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<ApiResponse<equipment_resource>>> InsertEquipmentResource([FromQuery] EquipmentResourceDto queryDto)
        {
            var result = await _service.InsertEquipmentResourceAsync(queryDto);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 修改设备
        /// </summary>
        /// <param name="queryDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult<ApiResponse<object>>> UpdateEquipment([FromQuery] EquipmentUpdateDto queryDto)
        {
            var result = await _service.UpdateEquipmentAsync(queryDto);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 根据id删除设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResponse<equipment?>>> DeleteEquipment([FromRoute] long id)
        {
            var result = await _service.DeleteEquipmentAsync(id);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 批量删除设备
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ActionResult<ApiResponse<List<string>>>> DeleteEquipmentList([FromQuery] List<long> idList)
        {
            var result = await _service.DeleteEquipmentListAsync(idList);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 根据id删除设备资料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResponse<equipment_resource?>>> DeleteEquipmentResource([FromRoute] long id)
        {
            var result = await _service.DeleteEquipmentResourceAsync(id);
            return new OkObjectResult(result);
        }

        /// <summary>
        /// 根据设备资料ID下载设备资料文件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<ApiResponse<IActionResult>>> DownloadEquipmentResourceById([FromRoute] long id)
        {
            try
            {
                var result = await _service.GetEquipmentResourceByIdAsync(id);
                if (result.Data == null)
                {
                    return new OkObjectResult(result);
                }
                else
                {
                    var filePath = result.Data.resource_path;
                    new FileExtensionContentTypeProvider().TryGetContentType(filePath, out string? contentType);
                    contentType ??= "application/octet-stream";
                    string fileName = Path.GetFileName(filePath);
                    string encodedFileName = Uri.EscapeDataString(fileName);

                    var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    return File(fileStream, contentType, encodedFileName);
                }
            }
            catch (Exception ex)
            {
                return new OkObjectResult(ApiResponse<IActionResult>.Error($"设备资料下载失败：{ex.Message}", 500));
            }
        }
    }

}
